package com.csi.controller.employee;

import com.alibaba.fastjson2.JSON;
import com.csi.domain.Employee;
import com.csi.pojo.Result;
import com.csi.service.EmployeeService;
import com.csi.service.impl.EmployeeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/employee/findByLoginAccountController")
public class FindByLoginAccountController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        EmployeeService employeeService = new EmployeeServiceImpl();
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter out = resp.getWriter();
        String loginAccount = req.getParameter("loginAccount");
        Employee employee = null;
        try {
            employee = employeeService.findByLoginAccount(loginAccount);
        } catch (Exception e) {
            e.printStackTrace();
            Result<Object> result = new Result<>();
            result.setCode(500);
            result.setMsg("服务器内部错误");
            out.print(JSON.toJSONString(result));
            out.flush();
            out.close();
            return;
        }
        if (employee == null) {
            Result<Object> result = new Result<>();
            result.setCode(404);
            result.setMsg("员工不存在");
            out.print(JSON.toJSONString(result));
            out.flush();
            out.close();
            return;
        }
        Result<Employee> result = new Result<>();
        result.setCode(200);
        result.setMsg("success");
        result.setDatas(employee);
        out.print(JSON.toJSONString(result));
        out.flush();
        out.close();
    }
}